local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")

local iori_jump = import('..views.iori_jump')
local iori_run = import('..views.iori_run')
local iori_run_action = import('..views.iori_run_action') 
local iori_jump_action = import('..views.iori_jump_action')
local iori_attack = import('..views.iori_attack')
local iori_attack2 = import('..views.iori_attack2')

local minren_model = import('..views.min_action')
local minren_testBody = import('..views.min_test')

local ball = import('..views.ball')

local world = class('world', function()
	return display.newNode()
end)


local currentChildTag
function world:ctor()
	cc.GameObject.extend(self):addComponent("components.behavior.EventProtocol"):exportMethods()
	self:addEventListener('selfonTouch',handler(self,self.receiverEvent))

	cc.ParticleSnow:create():addTo(self)
	--cc.ParticleGalaxy:create():addTo(self)
	
	local touch = display.newLayer():addTo(self)
	touch:setTouchEnabled(true)
	touch:addNodeEventListener(cc.NODE_TOUCH_EVENT,handler(self,self.onTouch))

	local listener = cc.EventListenerPhysicsContact:create()
	--listener:retain()
	listener:registerScriptHandler(handler(self,self.onContact),cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
	listener:registerScriptHandler(handler(self,self.onContact),cc.Handler.EVENT_PHYSICS_CONTACT_PRESOLVE)
	listener:registerScriptHandler(handler(self,self.onContact),cc.Handler.EVENT_PHYSICS_CONTACT_POSTSOLVE)
	listener:registerScriptHandler(handler(self,self.onContact),cc.Handler.EVENT_PHYSICS_CONTACT_SEPERATE)
	cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(listener,1)
	--self:setTag(1)
	--local jump = iori_jump.new():addTo(self)
	--jump:setTag(10)
	--local run = iori_run.new():addTo(self)
	--run:setTag(11)
	--local run_action = iori_run_action.new():addTo(self)
	--run_action:setTag(12)
	--local jump_action = iori_jump_action.new():addTo(self)
	--jump_action:setTag(13)
	--local attack_action = iori_attack.new():addTo(self)
	--attack_action:setTag(14)
	--local attack_action2 = iori_attack2.new():addTo(self)
	--attack_action2:setTag(15)
	--local minren = minren_model.new():addTo(self)
	--minren:setTag(16)
	local minren_test = minren_testBody.new():addTo(self)
	minren_test:setTag(17)

	currentChildTag = 17
	local state = 8		   
	local buttom = cc.ui.UIPushButton.new({normal = 'CloseNormal.png',pressed = 'CloseSelected.png'},{scale9=true})
					:setButtonSize(60,60)
					:onButtonClicked(function(event)
						--print(' ... buttom is on Clicked')
						---[[
						--print(event.name,event.x,event.y)
						local child = self:getChildByTag(16)
						if child and state ~= 5 then child:removeSelf() end
						if state == 1 then
							self:removeChildByTag(10, true)
							local run = iori_run.new():addTo(self)
							run:setTag(11)
							currentChildTag = 11
							state = 2
						elseif state == 2 then
							self:removeChildByTag(11, true)
							local run_action = iori_run_action.new():addTo(self)
							run_action:setTag(12)
							currentChildTag = 12
							state = 3
						elseif state == 3 then
							self:removeChildByTag(12, true)
							local jump_action = iori_jump_action.new():addTo(self)
							jump_action:setTag(13)
							currentChildTag = 13
							state = 4
						elseif state == 4 then
							self:removeChildByTag(13, true)
							local attack_action = iori_attack.new():addTo(self)
							attack_action:setTag(14)
							currentChildTag = 14
							state = 5
						elseif state == 5 then
							self:removeChildByTag(14, true)
							local attack_action2 = iori_attack2.new():addTo(self)
							attack_action2:setTag(15)
							currentChildTag = 15
							state = 6		
						elseif state == 6 then
							self:removeChildByTag(15, true)
							local minren = minren_model.new():addTo(self)
							minren:setTag(16)
							currentChildTag = 16
							state = 7						
						elseif state == 7 then
							self:removeChildByTag(16, true)
							local minren_test = minren_testBody.new():addTo(self)
							minren_test:setTag(17)
							currentChildTag = 17
							state = 8
						elseif state == 8 then
							self:removeChildByTag(17, true)
							local jump = iori_jump.new():addTo(self)
							jump:setTag(10)
							currentChildTag = 10
							state = 1	
						end
						--]]
					end)
					:addTo(self)
					:pos(display.width - 40,display.height - 40)
	local buttom2
	buttom2 = cc.ui.UIPushButton.new({normal = 'CloseNormal.png',pressed = 'CloseSelected.png'},{scale9=true})
					:setButtonSize(60,60)
					:onButtonClicked(function(event)
						if state == 3 then
							buttom:setButtonEnabled(false)
							buttom2:setButtonEnabled(false)
							self:removeChildByTag(12, true)
							local jump_action = iori_jump_action.new():addTo(self)
							jump_action:pos(display.cx-55,display.cy+75)
							jump_action:setTag(13)
							jump_action.iori:stopAction(jump_action.repeatAnimate)
							--jump_action.iori:runAction(jump_action.spawn)
							local seq = cc.Sequence:create(
								jump_action.spawn,
								cc.CallFunc:create(function() 
									buttom:setButtonEnabled(true)
									self:removeChildByTag(13, true)
									local run_action = iori_run_action.new():addTo(self)
									run_action:setTag(12)
									scheduler.performWithDelayGlobal(function()
										buttom2:setButtonEnabled(true)
										end, 0.3)
								end)
							)
							jump_action.iori:runAction(seq)
						elseif state == 5 then
							local child = self:getChildByTag(16)
							if child then child:removeSelf()
							else
								local minren = minren_model.new():addTo(self)
								minren:setTag(16)
							end
						else
							local child = self:getChildByTag(currentChildTag)
							child:setVisible(not child:isVisible())
						end
					end)
					:addTo(self)
					:pos(display.cx,display.cy - 200)





	self:dispatchEvent({name = "onTouch"})
end	

local points
function world:onTouch(event)
	--print('.. onTouch ..')
	--cc.Director:getInstance():resume()
	if event.name == 'began' then
		local y = event.y
		local aBall = ball.new(event.x,y):addTo(self)
		aBall:pos(0,y)
		if not aBall.body then print('... not have aBall.body ...')
		else
			aBall.body:setVelocity(cc.p(350,0))
			--aBall.body:setAngularVelocity(cc.p(100,0))
			--aBall.body:applyForce(cc.p(200,0))
		end

		self:dispatchEvent({name = "onTouch"})
		self:dispatchEvent({name = "selfonTouch"})

		points = 1
		return true
	elseif event.name == 'moved' then
		points = points + 1
	else
		printf('points number = %d',points)
	end
end

function world:onContact(event)
	--print(event:getEventCode(),3)
	if event:getEventCode() == 2 then
		--print('... Physicies on Contact ...')
		local a = event:getShapeA():getBody()
		local b = event:getShapeB():getBody()

		local tagA,tagB = a:getTag(),b:getTag()

		local removeBody = nil
		if tagA == WORLDBOX_TAG then removeBody = b
		elseif tagB == WORLDBOX_TAG then removeBody = a
		end

		if removeBody then
			removeBody:getNode():removeSelf()
			return false
		else
			return true
		end
		--[[
		else
			local d = event:getContactData()
			--printf('type of d : %s',type(d))
			for k,v in pairs(d.points[1]) do
				print(k,v)
			end
			local x1 = d.points[1].x
			local y1 = d.points[1].y
			local x = a:getPosition().x - x1
			local y = a:getPosition().y - y1

			a:applyImpulse(cc.p(4000*x,4000*y))
			--print('... Physicies on Contact ...')
			--printf('ShapeA tag=%d,ShapeB tag=%d',a:getTag(),b:getTag())
			--printf('contact point number:%d',d[count])
			--print(event.name)
			--cc.Director:getInstance():pause()
		end
		]]
		

	else
		return true
	end
end

function world:receiverEvent(event)
	print('world receiver event',event.name)
end

return world